﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class LogicMrg
{
    private static LogicMrg _ins;
    public static LogicMrg Instance
    {
        get
        {
            if(LogicMrg._ins == null) LogicMrg._ins = new LogicMrg();
            return LogicMrg._ins;
        }
    }
    private List<ItemCell> allow_list= new List<ItemCell>();//允许画线数据
    private List<ItemCell> items=new List<ItemCell> ();
    public List<ItemCell> cells { get { return items; } }
    public void AddCell(ItemCell cell)
    {
        items.Add (cell);
    }
    public void RemoveAll()
    {
        items.Clear ();
    }
    /// <summary>
    /// 获取相同类型的items
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public List<ItemCell> GetItemsByType(int type)
    {
        List<ItemCell> list = new List<ItemCell> ();
        foreach (ItemCell cell in items)
        {
            if(cell.data.type == type)list.Add (cell);
        }
        return list;
    }
    public List<ItemCell> GetItemsByNotType(int type)
    {
        List<ItemCell> list = new List<ItemCell>();
        foreach (ItemCell cell in items)
        {
            if (cell.data.type != type) list.Add(cell);
        }
        return list;
    }
    public void HideItemCellMask()
    {
        foreach (ItemCell cell in items) cell.ShowMask(false);
    }

    private bool _isAllow;
    /// <summary>
    /// 是否允许画线
    /// </summary>
    public bool isAllow
    {
        get { return _isAllow; }
        set
        {
            this._isAllow = value;
            if (!this._isAllow)
            {
                this.HideItemCellMask();
            }
        }
    }
    /// <summary>
    /// 检测是否允许画线
    /// </summary>
    /// <param name="cell"></param>
    /// <returns></returns>
    public bool CheckItemCellAllow(ItemCell cell)
    {
        foreach(ItemCell item in this.allow_list)
        {
            if(item == cell)return true;
        }
        return false;
    }
    /// <summary>
    /// 找出相同的类型
    /// </summary>
    /// <param name="type"></param>
    public void SetType(int type)
    {
        this.isAllow = true;
        this.HideItemCellMask();
        this.allow_list = this.GetItemsByType(type);
        List<ItemCell> list = this.GetItemsByNotType(type);
        for (int i = 0; i < list.Count; i++)
        {
            list[i].ShowMask(true);
        }
    }


}
